/*
 * Copyright (C) 2007 adrian_007, adrian-007 on o2 point pl
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef MPLIB_UTIL_H
#define MPLIB_UTIL_H

#include <map>
#include <vector>

typedef std::map<std::string, std::string> StringMap;
typedef StringMap::const_iterator StringMapIter;

typedef std::vector<std::string> StringList;
typedef StringList::const_iterator StringIter;

#if defined(_MSC_VER)
#define _LL(x) x##ll
#define _ULL(x) x##ull
#define I64_FMT "%I64d"
#define U64_FMT "%I64d"
#define snprintf _snprintf
#define snwprintf _snwprintf
#endif

#ifndef STDCALL
#define STDCALL __stdcall
#endif

class Util
{
public:
	typedef wstring (STDCALL *FuncToWide)	(string str);
	typedef string (STDCALL *FuncFromWide)	(wstring str);

	static void initialize(FuncToWide pfnTW, FuncFromWide pfnFW) {
		convertToWide = pfnTW;
		convertFromWide = pfnFW;
	}

	static string toString(short val) {
		char buf[8];
		snprintf(buf, sizeof(buf), "%d", (int)val);
		return buf;
	}
	static string toString(unsigned short val) {
		char buf[8];
		snprintf(buf, sizeof(buf), "%u", (unsigned int)val);
		return buf;
	}
	static string toString(int val) {
		char buf[16];
		snprintf(buf, sizeof(buf), "%d", val);
		return buf;
	}
	static string toString(unsigned int val) {
		char buf[16];
		snprintf(buf, sizeof(buf), "%u", val);
		return buf;
	}
	static string toString(long val) {
		char buf[32];
		snprintf(buf, sizeof(buf), "%ld", val);
		return buf;
	}
	static string toString(unsigned long val) {
		char buf[32];
		snprintf(buf, sizeof(buf), "%lu", val);
		return buf;
	}
	static string toString(long long val) {
		char buf[32];
		snprintf(buf, sizeof(buf), I64_FMT, val);
		return buf;
	}
	static string toString(unsigned long long val) {
		char buf[32];
		snprintf(buf, sizeof(buf), U64_FMT, val);
		return buf;
	}
	static string toString(double val) {
		char buf[16];
		snprintf(buf, sizeof(buf), "%0.2f", val);
		return buf;
	}

	static string formatSeconds(uint64_t aSec, bool supressHours = false) {
		char buf[64];
		if (!supressHours)
			snprintf(buf, sizeof(buf), "%01lu:%02d:%02d", (unsigned long)(aSec / (60*60)), (int)((aSec / 60) % 60), (int)(aSec % 60));
		else
			snprintf(buf, sizeof(buf), "%02d:%02d", (int)(aSec / 60), (int)(aSec % 60));	
		return buf;
	}

	static string getFileName(const string& path) {
		string::size_type i = path.rfind("\\");
		return (i != string::npos) ? path.substr(i + 1) : path;
	}

	static void tokenizeString(const string& str, const char* limiter, StringList& result);
	static string formatBytes(uint64_t aBytes);
	static int64_t getFileSize(const wstring& aFileName) throw();
	static double toDouble(const string& aString);
	static string formatParams(const string& format, StringMap& params);

	static FuncToWide convertToWide;
	static FuncFromWide convertFromWide;
};

#endif // MPLIB_UTIL_H